﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using PolePositionMobileUtilityData;
using System.Data;
using System.Data.SqlServerCe;
using PolePositionMobileUtilityEntity;
using PolePositionMobileUtilityEntity.Entities;
using PolePositionMobileUtilityGeneral;

namespace PolePositionMobileUtilityLogic
{
    public class LUtilityInspection
    {
        /// <summary>
        /// Get a Datatable with this Collumns Id,AcquisitionDate,Toptype_Name,InspectionResults_Name,Number,Result_Id
        /// </summary>
        /// <returns></returns>
        public static DataTable GetMainGrid()
        {
            DataTable _dt = DUtilityInspection.GetMainGrid();
            _dt.Columns.Add(new DataColumn("NumberPosition", typeof (Int32)));


            for(int i = 0; i<_dt.Rows.Count; i++)
            {
                _dt.Rows[i]["NumberPosition"] = i +1;
            }
            return _dt;
        }

        /// <summary>
        /// Return a DataRow with all InspectionData, if has error return null
        /// </summary>
        /// <param name="inspectionId"></param>
        /// <returns></returns>
        public static EUtilityInspection GetById(int inspectionId)
        {
            DataRow _dr = DUtilityInspection.GetById(inspectionId);

            EUtilityInspection _inspection = new EUtilityInspection();
            _inspection.AcquisitionDate = ConvertMobileHelper.ToDateTime(_dr["AcquisitionDate"]);
            _inspection.Class = new EClass(ConvertMobileHelper.ToInt32(_dr["ClassFk"]), ConvertMobileHelper.Tostring(_dr["Class_Name"]));
            _inspection.Company = new ECompany(ConvertMobileHelper.ToInt32(_dr["CompanyFk"]), ConvertMobileHelper.Tostring(_dr["Company_Name"]));
            _inspection.Id = ConvertMobileHelper.ToInt32(_dr["UtilityInspectionId"]);
            _inspection.Inspector = new EInspector(ConvertMobileHelper.ToInt32(_dr["InspectorFk"]), ConvertMobileHelper.Tostring(_dr["Inspector_Name"]));
            _inspection.Latitude = ConvertMobileHelper.Todouble(_dr["Latitude"]);
            _inspection.Longitude = ConvertMobileHelper.Todouble(_dr["Longitude"]);
            _inspection.Length = new ELength(ConvertMobileHelper.ToInt32(_dr["LengthFk"]), ConvertMobileHelper.Tostring(_dr["Length_Name"]));
            _inspection.LVisit = ConvertMobileHelper.ToDateTime(_dr["LVisit"]);
            _inspection.Map = new EMap(ConvertMobileHelper.ToInt32(_dr["MapFk"]), ConvertMobileHelper.Tostring(_dr["Map_Name"]));
            _inspection.Section = new ESection(ConvertMobileHelper.ToInt32(_dr["SectionFk"]), "",_inspection.Map);
            _inspection.Number = ConvertMobileHelper.Tostring(_dr["Number"]);
            _inspection.Specie = new ESpecie(ConvertMobileHelper.ToInt32(_dr["SpecieFk"]), ConvertMobileHelper.Tostring(_dr["Specie_Name"]));
            _inspection.Toptype = new EToptype(ConvertMobileHelper.ToInt32(_dr["ToptypeFk"]), ConvertMobileHelper.Tostring(_dr["Toptype_Name"]));
            _inspection.Treatment = new ETreatment(ConvertMobileHelper.ToInt32(_dr["TreatmentFk"]), ConvertMobileHelper.Tostring(_dr["Treatment_Name"]));
            _inspection.VisitType = new EVisitType(ConvertMobileHelper.ToInt32(_dr["VisitTypeFk"]), ConvertMobileHelper.Tostring(_dr["VisitType_Name"]));
            _inspection.Year = ConvertMobileHelper.ToInt32(_dr["Year"]);
            _inspection.Images = LImage.GetAllByInspection(_inspection.Id);
            _inspection.GuardsQuantity = ConvertMobileHelper.ToInt32(_dr["GuardsQuantity"]);
            _inspection.PoleTag = (bool)_dr["PoleTag"];
            _inspection.Billable = (bool)_dr["Billable"];
            _inspection.GroundWireRepair = (bool)_dr["GroundWireRepair"];
            _inspection.LastCompany = ConvertMobileHelper.Tostring(_dr["LastCompany"]);
            return _inspection;
            

        }

        public static int Insert(EUtilityInspection inspection)
        {
            inspection.Id = DUtilityInspection.Insert(inspection.Map.Id, inspection.Latitude, inspection.Longitude, inspection.Year, inspection.LVisit, inspection.Number, inspection.AcquisitionDate, inspection.Treatment.Id, inspection.Class.Id, inspection.Specie.Id, inspection.Toptype.Id, inspection.Length.Id, inspection.Inspector.Id, inspection.Company.Id,inspection.Section.Id, inspection.GuardsQuantity, inspection.LastCompany, inspection.VisitType.Id, inspection.PoleTag, inspection.Billable, inspection.GroundWireRepair);
            LImage.Insert(inspection.Images,inspection.Id);
            return inspection.Id;
        }

        public static void Update(EUtilityInspection inspection, SqlCeTransaction tr)
        {
            LImage.DeleteByInspection(inspection.Id,inspection.Images);
            LImage.Insert(inspection.Images, inspection.Id);
            DUtilityInspection.Update(inspection.Id, inspection.Map.Id, inspection.Latitude, inspection.Longitude, inspection.Year, inspection.LVisit, inspection.Number, inspection.AcquisitionDate, inspection.Treatment.Id, inspection.Class.Id, inspection.Specie.Id, inspection.Toptype.Id, inspection.Length.Id, inspection.Inspector.Id, inspection.Company.Id, inspection.Section.Id, inspection.GuardsQuantity, inspection.LastCompany, inspection.VisitType.Id, inspection.PoleTag, inspection.Billable, inspection.GroundWireRepair,  tr);
        }

        public static void Delete(EUtilityInspection inspection, SqlCeTransaction tr)
        {
            DUtilityInspection.Delete(inspection.Id, tr);
        }

    }
}
